<template>
  <el-dialog
    title="分配权限"
    width="600px"
    :visible.sync="isShow"
    @close="closeEvent"
  >
    <!-- 树形结构 -->
    <el-tree
      ref="tree"
      node-key="id"
      :data="treeData"
      :props="{ label: 'name' }"
      show-checkbox
      check-strictly
    />
    <template #footer>
      <div>
        <el-button type="primary" @click="submmit">确定</el-button>
        <el-button>取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermission } from '@/api/permisse'
import { sysRoleGet, sysRoleAssignPremPut } from '@/api/setting'
export default {
  data() {
    return {
      isShow: false,
      treeData: [],
      id: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    //   获取所有权限点数据
    async getData() {
      const res = await sysPermission()
      this.treeData = this.changeData(res.data, '0')
    },
    // 处理数据成为树形结构
    changeData(arr, str) {
      return arr.filter((item) => {
        if (item.pid === str) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // 角色的权限数据
    async getPermmisionData(id) {
      this.id = id
      const res = await sysRoleGet(id)
      //   角色权限的默认勾选
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    closeEvent() {
      this.$refs.tree.setCheckedKeys([])
    },
    // 提交权限修改
    async submmit(id) {
      await sysRoleAssignPremPut({
        id: this.id,
        permIds: this.$refs.tree.getCheckedKeys() // 拿到已勾选数据
      })
      //   提示修改成功
      this.$message.success('修改成功')
      //   关闭弹框
      this.isShow = false
    }
  }
}
</script>

<style></style>
